<?php

//展示销售排名模块，Input = NULL，Output = 本店的销售排名
header("content-type:application/json;charset=utf-8");

include_once "0.1conn.php"; //连接数据库

session_start();
$shop_id = $_SESSION['user']['shop_id'];
$query = "SELECT salesman,total_price FROM sale_record WHERE shop_id = '$shop_id'";

mysqli_query($conn, "SELECT * FROM userlist WHERE shop_id = '$shop_id'");

$arr63 = array();
if ($result = mysqli_query($conn, $query)) {
    $all_price = array();
    while ($row = mysqli_fetch_assoc($result)) {
        $sale_man = $row['salesman'];
        $sale_price = $row['total_price'];
        if (isset($all_price[$sale_man])) {
            $all_price[$sale_man] += $sale_price;
        } else {
            $all_price[$sale_man] = $sale_price;
        }
    }
    //根据销售额降序排序
    arsort($all_price);
    //把信息打包为json数组发给前端
    $arr721 = array();
    $rank = 1;
    //前端field为'rank','name'和'price'
    foreach ($all_price as $man => $price_n) {
        $arr721[] = array('rank' => $rank, 'name' => $man, 'price' => $price_n);
        $rank += 1;
    }
}
echo json_encode($arr721);
//关闭连接
mysqli_close($conn);

